<template>
  <div class="login_container">
    <div class="login_box">
      <!--logo区域-->
      <div class="login_logo_box">
        <img :src="require('@/assets/img/logo.png')" alt="">
      </div>
      <!--表单区域-->
      <!--:model为v-bind:model的缩写-->
      <el-form ref="loginFormRef" :model="loginForm" :rules="rules" class="login_form" label-width="0">
        <!--用户名-->
        <el-form-item prop="id">
          <!--v-model为v-model:value的缩写-->
          <el-input @keyup.enter.native="PreValidation" v-model="loginForm.id" prefix-icon="el-icon-user" placeholder="用户名" clearable></el-input>
        </el-form-item>
        <!--密 码-->
        <el-form-item prop="password">
          <el-input @keyup.enter.native="PreValidation" type="password" v-model="loginForm.password" prefix-icon="el-icon-lock" placeholder="密码" show-password clearable></el-input>
        </el-form-item>
        <!--按钮-->
        <el-form-item>
          <el-button type="primary" @click="PreValidation">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="login_identity_box">
      <!--身 份-->
      <el-radio-group v-model="radio">
        <el-radio :label="1" border>学生</el-radio>
        <el-radio :label="2" border>教师</el-radio>
        <el-radio :label="3" border>管理员</el-radio>
      </el-radio-group>
    </div>
  </div>
</template>

<script>
import {loginStudent} from "@/api/student";
import {loginTeacher} from "@/api/teacher";
import {loginAdministrator} from "@/api/administrator";

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Login",
  data() {
    return{
      radio: 1,
      loginForm: {},
      rules: {
        id: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    //重置按钮，重置登录表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    //表单预验证
    PreValidation(){
      this.$refs.loginFormRef.validate(async valid=>{
        if (!valid) return;
        if (this.radio===1){//学生登陆
          loginStudent({name:this.loginForm.id,password:this.loginForm.password}).then(res => {
            if (res.code===0){
              let user = JSON.stringify(res.data.user);
              window.sessionStorage.setItem("user",user);
              this.$router.push("/index");
              this.$message.success(res.message);
            }else{
              this.$message.error(res.message);
              this.loginForm.password='';
            }
          }).catch(res => {
            this.$message.error(res.code);
          })
        }else if (this.radio===2){//教师登陆
          loginTeacher({name:this.loginForm.id,password:this.loginForm.password}).then(res => {
            if (res.code===0){
              let user = JSON.stringify(res.data.user);
              window.sessionStorage.setItem("user",user);
              this.$router.push("/index");
              this.$message.success(res.message);
            }else{
              this.$message.error(res.message);
              this.loginForm.password='';
            }
          }).catch(res => {
            this.$message.error(res.code);
          })
        }else {//管理员登陆
          loginAdministrator({name:this.loginForm.id,password:this.loginForm.password}).then(res => {
            if (res.code===0){
              let user = JSON.stringify(res.data.user);
              window.sessionStorage.setItem("user",user);
              this.$router.push("/");
              this.$message.success(res.message);
            }else{
              this.$message.error(res.message);
              this.loginForm.password='';
            }
          }).catch(res => {
            this.$message.error(res.code);
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>

.login_container{
  height: 100%;
  width: 100%;
}

.login_box{
  width: 450px;
  height: 300px;
  background-color: white;
  border-radius: 10px;
  box-shadow: 0 0 10px #ddd;
  //居中开始
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
  //居中结束
  text-align: center;
}

.login_logo_box{
  width: 100px;
  height: 100px;
  border: solid 1px #eee;
  border-radius: 50%;
  padding: 5px;
  //外圈阴影
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%,-50%);
  background-color: white;
  img{
    background-color: #eee;
    border-radius: 50%;
    width: 100%;
    height: 100%;
  }
}

.login_form{
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 30px;
  box-sizing: border-box;
}

.login_identity_box{
  width: 450px;
  height: 50px;
  background-color: white;
  border-radius: 15px;
  box-shadow: 0 0 10px #ddd;
  //居中开始
  position: absolute;
  left: 50%;
  top: 70%;
  transform: translate(-50%,-50%);
  //居中结束
  display:flex;
  align-items: center;/*垂直居中*/
  justify-content: center;/*水平居中*/
}

</style>
